package com.cars.service;

import com.cars.entity.Coffee;
import jakarta.annotation.PostConstruct;
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.stereotype.Component;

/**
 * @author: caoqianfan
 * @date: 2023/12/20 14:48
 */
@Component
public class CoffeeLoader {
    private final ReactiveRedisConnectionFactory factory;
    private final ReactiveRedisOperations<String, Coffee> coffeeOps;

    public CoffeeLoader(ReactiveRedisConnectionFactory factory, ReactiveRedisOperations<String, Coffee> coffeeOps) {
        this.factory = factory;
        this.coffeeOps = coffeeOps;
    }

    @PostConstruct
    public void loadData() {
        factory.getReactiveConnection().serverCommands().flushAll()
//                .thenMany(
//                        Flux.just("Jet Black Redis", "Darth Redis", "Black Alert Redis")
//                                .map(name -> new Coffee(UUID.randomUUID().toString(), name))
//                                .flatMap(coffee -> coffeeOps.opsForValue().set(coffee.getId(), coffee))
//                )
                .thenMany(coffeeOps.keys("*")
                        .flatMap(coffeeOps.opsForValue()::get))
                .subscribe(System.out::println);
    }
}